import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class LoginPage extends Utils {
    private static String url = "http://8.137.60.68:9091/gobang_login.html";
    public LoginPage() {
        super(url);
    }

    public void loginSuc(String username, String password) {

        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();
        // 查看是否登陆成功
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#match-button")));
        assert el.getText().equals("开始匹配");
        // 登陆完成后回到原始页面
        driver.navigate().back();
    }

    public void loginFail(String username, String password) {
        driver.navigate().refresh();
        // 账号错误
        driver.findElement(By.cssSelector("#username")).sendKeys(username);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#submit")).click();
        // 切换到弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
}
